//
//  OCSSLoginPage.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/23.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import SwiftyJSON

class OCSSLoginPage: OCSSBaseViewController {

    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func loginButtonEvent(_ sender: UIButton) {
        if usernameTF.text == "" {
            return;
        }
        if passwordTF.text == "" {
            return;
        }
        requestForLogin();
    }
//    MARK:login
    func requestForLogin() -> Void {
        let paras:[String:String] = ["username":usernameTF.text!,
                                     "password":passwordTF.text!];
        OCSSNetRequest.requestData(url: "/account/login", para: paras, type: MethodType.post, isLogin: true, success: { (result) in
            let resDict = JSON(result).dictionaryValue;
            if (resDict["Status"]?.intValue == 1){
                UserDefaults.standard.set(true, forKey: kLoginStatus);
                UserDefaults.standard.set(resDict["Data"], forKey: "kData");
                _ = UserDefaults.synchronize(UserDefaults.standard);
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: kReloadRootPage), object: true);
            }
            
        }) { (error) in
            print("登录失败");
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
